package com.ruoyi.web.controller.net;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.net.domain.DeviceStatusIsisNeighb;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.DeviceStatusAprNeighb;
import com.ruoyi.net.service.IDeviceStatusAprNeighbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 设备状态-APR邻居Controller
 * 
 * @author tony
 * @date 2024-12-11
 */
@Api(tags = "设备状态-APR邻居")
@RestController
@RequestMapping("/device/status/apr/neighb")
public class DeviceStatusAprNeighbController extends BaseController
{
    @Autowired
    private IDeviceStatusAprNeighbService deviceStatusAprNeighbService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 读取设备状态-APR邻居列表
     */
    @ApiOperation(value = "读取设备状态-APR邻居列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:neighb:list')")
    @GetMapping("/read")
    public TableDataInfo read(DeviceStatusAprNeighb deviceStatusAprNeighb)
    {
        List<DeviceStatusAprNeighb> deviceStatusAprNeighbs = new ArrayList<>();
        StringBuilder xml = new StringBuilder();

        xml.append("<filter type=\"subtree\">" +
                "<arp-oper xmlns=\"http://fiberhome.com/fhnw/yang/ipservice/fos-arp-oper\">" +
                "      <arp-items>" +
                "        <arp-item>");
//                "<vrf-name>FHNMVPN</vrf-name>" + // 查询 条件
//                " <arp-type>interface</arp-type>"+
//                " <ip-addr>10.10.1.2</ip-addr>"+
//                " <if-name>dcn-serial-flexe-tnl1</if-name>"+
        if (StringUtils.isNotEmpty(deviceStatusAprNeighb.getVrfName())) {
            xml.append("     <vrf-name>")
                    .append(deviceStatusAprNeighb.getVrfName())
                    .append("</vrf-name>");
        }
        if (StringUtils.isNotEmpty(deviceStatusAprNeighb.getArpType())) {
            xml.append("     <arp-type>")
                    .append(deviceStatusAprNeighb.getArpType())
                    .append("</arp-type>");
        }
        if (StringUtils.isNotEmpty(deviceStatusAprNeighb.getIpAddr())) {
            xml.append("     <ip-addr>")
                    .append(deviceStatusAprNeighb.getIpAddr())
                    .append("</ip-addr>");
        }
        if (StringUtils.isNotEmpty(deviceStatusAprNeighb.getPortName())) {
            xml.append("     <if-name>")
                    .append(deviceStatusAprNeighb.getPortName())
                    .append("</if-name>");
        }
                xml.append("        </arp-item>" +
                "      </arp-items>" +
                "</arp-oper>" +
                "</filter>");
        NetElement netElement = netElementService.getCacheObject(deviceStatusAprNeighb.getElementId());
        if (sysConfigService.selectDebugEnabled()) {
            deviceManager.getDevice(netElement.getIp());
            String rpcReply = deviceManager.getStatus(xml.toString());

            // 测试数据
            /*String rpcReply = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" last-modified=\"2018-04-09T10:54:21Z\" message-id=\"3\">" +
                    "         <data>" +
                    "         <arp-oper xmlns=\"http://fiberhome.com/fhnw/yang/ipservice/fos-arp-oper\">" +
                    "          <arp-items>" +
                    "           <arp-item>" +
                    "            <vrf-name>FHNMVPN</vrf-name>" +
                    "            <ip-addr>10.10.1.2</ip-addr>" +
                    "            <mac-addr>00:00:00:00:00:00</mac-addr>" +
                    "            <arp-type>interface</arp-type>" +
                    "            <expire-time>0</expire-time>" +
                    "            <if-name>dcn-serial-flexe-tnl1</if-name>" +
                    "           </arp-item>" +
                    "          </arp-items>" +
                    "         </arp-oper>" +
                    "         </data>" +
                    "        </rpc-reply>";*/
            // 解析xml
            NodeList list = findNetConfXmlNodes(rpcReply, "//arp-items/arp-item");
            if (list != null && list.getLength() > 0) {
                for (int i = 0; i < list.getLength(); i++) {
                    DeviceStatusAprNeighb entity = new DeviceStatusAprNeighb();
                    Node node = list.item(i);
                    // 通过标签名获取节点的值
                    entity.setVrfName(getXmlLabelValue(node, "vrf-name"));
                    entity.setIpAddr(getXmlLabelValue(node, "ip-addr"));
                    entity.setArpType(getXmlLabelValue(node, "arp-type"));
                    entity.setMacAddr(getXmlLabelValue(node, "mac-addr"));
                    entity.setPortName(getXmlLabelValue(node, "if-name"));
                    entity.setExpireTime(getXmlLabelValue(node, "expire-time"));
                    entity.setSvlanId(getXmlLabelValue(node, "svlan-id"));
                    entity.setCvlandId(getXmlLabelValue(node, "cvlan-id"));

                    deviceStatusAprNeighbs.add(entity);
                }
            }
        }
        startPage();
        return getDIYPageTable(deviceStatusAprNeighbs);
    }

    /**
     * 查询设备状态-APR邻居列表
     */
    @ApiOperation(value = "查询设备状态-APR邻居列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:neighb:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceStatusAprNeighb deviceStatusAprNeighb)
    {
        startPage();
        List<DeviceStatusAprNeighb> list = deviceStatusAprNeighbService.selectDeviceStatusAprNeighbList(deviceStatusAprNeighb);
        return getDataTable(list);
    }

    /**
     * 导出设备状态-APR邻居列表
     */
    @ApiOperation(value = "导出设备状态-APR邻居列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:neighb:export')")
    @Log(title = "设备状态-APR邻居", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceStatusAprNeighb deviceStatusAprNeighb)
    {
        List<DeviceStatusAprNeighb> list = deviceStatusAprNeighbService.selectDeviceStatusAprNeighbList(deviceStatusAprNeighb);
        ExcelUtil<DeviceStatusAprNeighb> util = new ExcelUtil<DeviceStatusAprNeighb>(DeviceStatusAprNeighb.class);
        util.exportExcel(response, list, "设备状态-APR邻居数据");
    }

    /**
     * 获取设备状态-APR邻居详细信息
     */
    @ApiOperation(value = "获取设备状态-APR邻居详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:neighb:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deviceStatusAprNeighbService.selectDeviceStatusAprNeighbById(id));
    }

    /**
     * 新增设备状态-APR邻居
     */
    @ApiOperation(value = "新增设备状态-APR邻居", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:neighb:add')")
    @Log(title = "设备状态-APR邻居", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceStatusAprNeighb deviceStatusAprNeighb)
    {
        return toAjax(deviceStatusAprNeighbService.insertDeviceStatusAprNeighb(deviceStatusAprNeighb));
    }

    /**
     * 修改设备状态-APR邻居
     */
    @ApiOperation(value = "修改设备状态-APR邻居", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('net:neighb:edit')")
    @Log(title = "设备状态-APR邻居", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceStatusAprNeighb deviceStatusAprNeighb)
    {
        return toAjax(deviceStatusAprNeighbService.updateDeviceStatusAprNeighb(deviceStatusAprNeighb));
    }

    /**
     * 删除设备状态-APR邻居
     */
    @ApiOperation(value = "删除设备状态-APR邻居", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('net:neighb:remove')")
    @Log(title = "设备状态-APR邻居", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceStatusAprNeighbService.deleteDeviceStatusAprNeighbByIds(ids));
    }
}
